<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn"> 关灯</button>
    <script>
        var btn = document.querySelector('button');
        var body = document.querySelector('body')
        flag = 1;
        btn.onclick = function () {
            if (flag == 1) {
                body.style.backgroundColor = 'black';
                btn.innerHTML = '开灯';
                flag=0;
            }else{
                body.style.backgroundColor = 'white';
                btn.innerHTML = '关灯';
                flag=1;
            }

        }

    </script>
</body>

</html>